/**
 * 
 */
package com.gwcloud.leetcode.hot100;

import java.util.Stack;

/**
 * @ClassName   : Leetcode155 
 * @author      : ganwei 
 * @date        : 2020年4月30日
 * @Description : Leetcode 155. 最小栈
 */
public class Leetcode0155 {
	public static void main(String args[]) {
		MinStack stack = new MinStack();
		stack.push(2);
		stack.push(0);
		stack.push(3);
		stack.push(0);
		System.out.println(stack.getMin());
		stack.pop();
		System.out.println(stack.getMin());
		stack.pop();
		System.out.println(stack.getMin());
		stack.pop();
		System.out.println(stack.getMin());
	}
}

/**
 * -解题思路-存储最小值,利用ArrayList实现最小栈
 */
class MinStack {
	
	private Stack<Integer> stack;
	private int minval;

    /** initialize your data structure here. */
    public MinStack() {
    	stack = new Stack<Integer>();
    	minval = Integer.MAX_VALUE;
    }
    
    /**
     * - 压栈时，记录最小值
     */
    public void push(int x) {
    	if(x <= minval) {
    		stack.push(minval);
    		minval = x;
    	}
        stack.push(x);
    }
    
    public void pop() {
    	if(stack.peek() == this.minval) {
    		stack.pop();
            this.minval = stack.peek();
    	}
    	stack.pop();
    }
    
    public int top() {
    	return stack.peek();
    }
    
    public int getMin() {
    	return this.minval;
    }
}

